package com.sad.we11cis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ProductListview extends Activity {

	String selectedCategory;
	
	private ListView productListView = null;
	private ListViewAdapter prodLvAdapter = null;
	
	Bundle extra;
	Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_product_listview);
		
		extra = new Bundle();
		intent = new Intent();
		
		productListView = (ListView) findViewById(R.id.categoryListview);
		
		GetCategoryTask getCategoryTask = new GetCategoryTask();
		getCategoryTask.execute();

		prodLvAdapter = new ListViewAdapter(this);
		productListView.setAdapter(prodLvAdapter);
		
		productListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				CategoryData mData = prodLvAdapter.mListData.get(arg2);
				selectedCategory = mData.Title;
				extra.putString("data", selectedCategory);
				intent.putExtras(extra);
				finishActivity();
			}
		});
	}
	
	private void finishActivity(){
		this.setResult(RESULT_OK, intent);
		this.finish();
	}
	
	private class GetCategoryTask extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				URL url = new URL("http://192.168.43.64:8080/Weening11evenServer/CommunicateServlet");
				URLConnection connection = url.openConnection();

				//send request to server
				connection.setDoOutput(true);
				OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
				out.write("For Category/");
				out.close();

				//receive result of login from server
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				String receivedBrand;
				while((receivedBrand = in.readLine()) != null){
					prodLvAdapter.addItem(receivedBrand);
				}
				in.close();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	}

	private class ViewHolder {
		public TextView mText;
	}

	private class ListViewAdapter extends BaseAdapter {

		private Context context = null;
		private ArrayList<CategoryData> mListData = new ArrayList<CategoryData>();

		public ListViewAdapter(Context context) {
			super();
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mListData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mListData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public void addItem(String mTitle) {
			CategoryData addInfo = null;
			addInfo = new CategoryData();
			addInfo.Title = mTitle;
			

			mListData.add(addInfo);
		}

		public void remove(int position) {
			mListData.remove(position);
			dataChange();
		}

		public void sort() {
			Collections.sort(mListData, CategoryData.ALPHA_CO_COMPARATOR);
			dataChange();
		}

		public void dataChange() {
			prodLvAdapter.notifyDataSetChanged();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();

				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.listview_category, null);

				holder.mText = (TextView) convertView
						.findViewById(R.id.prodName);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			CategoryData Data = mListData.get(position);


			holder.mText.setText(Data.Title);
			
			return convertView;
		}
	}
}
